package com.simulation.web.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simulation.common.utils.SecurityUtils;
import com.simulation.web.dto.TechReplyDTO;
import com.simulation.web.domain.TechReply;
import com.simulation.web.mapper.ReplyMapper;
import com.simulation.web.service.ReplyService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@AllArgsConstructor
public class ReplyServiceImpl implements ReplyService {

    private final ReplyMapper replyMapper;

    @Override
    public PageInfo<TechReply> getPage(TechReplyDTO techReplyDTO) {
        PageHelper.startPage(techReplyDTO.getPageNum(), techReplyDTO.getPageSize());
        List<TechReply> postList = replyMapper.getPage(techReplyDTO);
        return new PageInfo<>(postList);
    }

    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    @Override
    public List<TechReply> listAll() {
        return replyMapper.listAll();
    }


    /**
     * 根据主键查询
     *
     * @param id 主键
     * @return 返回记录，没有返回null
     */
    @Override
    public TechReply getById(Long id) {
        return replyMapper.getById(id);
    }

    /**
     * 新增，插入所有字段
     *
     * @param techReply 新增的记录
     * @return 返回影响行数
     */
    @Override
    public int insert(TechReply techReply) {
        techReply.setReplyTime(new Date());
        return replyMapper.insert(techReply);
    }

    /**
     * 新增，忽略null字段
     *
     * @param techReply 新增的记录
     * @return 返回影响行数
     */
    @Override
    public int insertIgnoreNull(TechReply techReply) {
        techReply.setReplyTime(new Date());
        techReply.setUserId(SecurityUtils.getUserId());
        return replyMapper.insertIgnoreNull(techReply);
    }

    /**
     * 修改，修改所有字段
     *
     * @param techReply 修改的记录
     * @return 返回影响行数
     */
    @Override
    public int update(TechReply techReply) {
        return replyMapper.update(techReply);
    }

    /**
     * 修改，忽略null字段
     *
     * @param techReply 修改的记录
     * @return 返回影响行数
     */
    @Override
    public int updateIgnoreNull(TechReply techReply) {
        return replyMapper.updateIgnoreNull(techReply);
    }

    /**
     * 删除记录
     *
     * @param id 待删除的记录
     * @return 返回影响行数
     */
    @Override
    public int delete(Long id) {
        // 删除当前回复下的所有回复
        replyMapper.deleteByReplyId(id);
        // 删除回复本身
        return replyMapper.deleteById(id);
    }

    @Override
    public int deleteByPostId(Long postId) {
        return replyMapper.deleteByPostId(postId);
    }

    @Override
    public int deleteByCommentId(Long commentId) {
        return replyMapper.deleteByCommentId(commentId);
    }


}